home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / futils / futils~1 / utils / mint.zoo / bg.c next >
Encoding:
C/C++ Source or Header  |  1991-11-11  |  1.1 KB  |  68 lines

  1. /*
  2.  * compiling this requires the GCC library, and/or the spawnvp found
  3.  * in the init directory
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <process.h>
  8. #include <osbind.h>
  9. #include <errno.h>
  10. #include <mintbind.h>
  11.  
  12. void
  13. usage()
  14. {
  15.     fprintf(stderr, "Usage: bg [-o file] program [args ... ]\n");
  16.     exit(2);
  17. }
  18.  
  19. int
  20. main(argc, argv)
  21.     int argc;
  22.     char **argv;
  23. {
  24.     long r;
  25.     int olderr;
  26.     int oldpgrp;
  27.     char *name;
  28.  
  29.     if (!argv[1]) {
  30.         usage();
  31.     }
  32.  
  33.     olderr = Fdup(2);
  34.     argv++;
  35.     name = *argv;
  36.     if (!strcmp(name, "-o")) {
  37.                 static char oname[1024];
  38.         if (!*argv) usage();
  39.         unx2dos(*++argv,oname);
  40.         r = Fcreate(oname, 0);
  41.         if (r < 0) {
  42.             errno = -r;
  43.             perror(oname);
  44.             exit(1);
  45.         }
  46.         name = *++argv;
  47.         if (!name) usage();
  48.     /* redirect stdout, stderr, and the control terminal (-1) */
  49.         Fforce(-1, r);
  50.         Fforce(1, r);
  51.         Fforce(2, r);
  52.     }
  53.  
  54. /* put child process into a new process group */
  55.     oldpgrp = Pgetpgrp();
  56.     Psetpgrp((int)Pgetpid(), (int)Pgetpid());
  57.     r = spawnvp(P_NOWAIT, name, argv);
  58.     Psetpgrp((int)Pgetpid(), oldpgrp);
  59.  
  60.     Fforce(2, olderr);
  61.  
  62.     if (r < 0) {
  63.         perror(name);
  64.         exit(2);
  65.     }
  66.     exit(r);
  67. }
  68.